package com.team.arms.tijian.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.team.arms.common.Result;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.util.ConvertUtil;
import com.team.arms.political.service.PoliReviewInfoService;
import com.team.arms.tijian.entity.vo.AllVO;
import com.team.arms.tijian.entity.vo.RecResultVO;
import com.team.arms.tijian.entity.vo.RecommendVo;
import com.team.arms.tijian.mapper.RecommendedMapper;
import com.team.arms.tijian.mapper.SysHealthExaminationMapper;
import com.team.arms.tijian.service.IRecommendedService;
import entity.Recommended;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 主检推荐复检表 前端控制器
 * </p>
 *
 * @author Huazige
 * @since 2022-06-02
 */
@RestController
@RequestMapping("/recommended")
@Api(tags="体检-主检推荐模块")
@CrossOrigin
public class RecommendedController {
   @Autowired
    RecommendedMapper recommendedMapper;

    @Autowired
    IRecommendedService iRecommendedService;

    @Autowired
    PoliReviewInfoService poliReviewInfoService;

    @Resource
    SysHealthExaminationMapper sysHealthExaminationMapper;
    @Autowired
    ConvertUtil convertUtil;


    @ApiOperation("获取主检推荐信息")
//    @Log(title = "政审信息", businessType = BusinessType.)
    @GetMapping(value = "/getrecommend")
    public AjaxResult getrecommend() {

        List<RecommendVo> list = recommendedMapper.selectrecommend();
//

        if (list != null) {
            return AjaxResult.success("查询成功", list);
        } else {
            return AjaxResult.error("查询失败");
        }
    }
    @ApiOperation(value = "主检推荐信息-下拉框查找")
    @GetMapping("/found")
    public AjaxResult getrecommendbyname(@RequestParam(value = "查询的字段") String field , @RequestParam(value = "查询的内容")String content) {
        //也可以直接从redis缓冲中去调用一个foreach循环拿到对应name的数据

        List<RecommendVo> list = recommendedMapper.selectrecommendbyname(field,content);

        if (list.size()>0){
            return AjaxResult.success("查找成功",list);
        }else {
            return    AjaxResult.success("没有找到信息");
        }

    }
    @ApiOperation("excel导入主检推荐信息")
    @PostMapping("/importMilitary")
    @ResponseBody
    public Result importStudent(MultipartFile file) throws Exception
    {
        return iRecommendedService.importExcel(file);
    }
    //导出，导入，主键结论

    @ApiOperation("导出所有主检推荐信息")
    @GetMapping("/export")
    public void exportfailed(HttpServletResponse response) {
        List<RecommendVo> list = recommendedMapper.selectrecommend();

//        List<Policenvo> list = poliReviewInfoMapper.selectall(poliCarefulState);
//
        poliReviewInfoService.exportpoli(response, list, RecommendVo.class,"主检推荐信息表");

    }
    @ApiOperation("获取某个人的详情内容")
    @GetMapping(value = "/getall/{examineId}}")
    public AjaxResult selectall(
            @PathVariable("examineId")String examineId
    ) {

        List<AllVO> list = sysHealthExaminationMapper.select(examineId);
        return AjaxResult.success("查询成功",list) ;

    }

    @ApiOperation("获取主检结论")
    @GetMapping(value = "/get/{examineId}}")
    public AjaxResult getresult(
            @PathVariable("examineId")String examineId
    ) {
        QueryWrapper<entity.Recommended> wrapper2 = new QueryWrapper();
        wrapper2.eq("examine_id",examineId);
        Recommended recommended = recommendedMapper.selectOne(wrapper2);

        RecResultVO convert = convertUtil.convert(recommended, RecResultVO.class);

        return AjaxResult.success("查询成功",convert) ;

    }

    @ApiOperation("添加主检结论")
    @PostMapping(value = "/add/{examineId}}")
    public AjaxResult addresult(
            @PathVariable("examineId")String examineId,
            @RequestBody RecResultVO recResultVO
    ) {
        QueryWrapper<entity.Recommended> wrapper2 = new QueryWrapper();
        wrapper2.eq("examine_id",examineId);


        Recommended recommended = recommendedMapper.selectOne(wrapper2);
        recommended.setFailedName(recResultVO.getFailedName());
        recommended.setSuggest1(recResultVO.getSuggest1());
        recommended.setSuggest2(recResultVO.getSuggest2());
        recommended.setIsCurable1(recResultVO.getIsCurable1());
        recommended.setIsCurable2(recResultVO.getIsCurable2());
        recommended.setFailedName2(recResultVO.getFailedName2());
//        Recommended convert = convertUtil.convert(recResultVO, Recommended.class);
        recommendedMapper.update(recommended, wrapper2);

        return AjaxResult.success("添加成功",recResultVO) ;

    }
    @ApiOperation("复检/单个+批量删除")
    @DeleteMapping("/examineId")
    public Result deleteInformation(
            @ApiParam("体检编号") @RequestParam("examineId")List<String> deletelist ) {
        int delete =0;

        for ( String id:deletelist
        ) {
            QueryWrapper<entity.SysHealthExamination> wrapper = new QueryWrapper();
            wrapper.eq("examine_id",id);

            delete = sysHealthExaminationMapper.delete(wrapper);


        }
        if (delete>0){
            return Result.success("批量删除成功");
        }
        return Result.error("批量删除失败");
    }
}

